package com.ayf.payment.game.api.controller.player;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.dto.AybUserDTO;
import com.ayf.payment.game.api.dto.PartitionDTO;
import com.ayf.payment.game.api.entity.AybUser;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.entity.Order;
import com.ayf.payment.game.api.service.AybUserService;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.MerchantSonService;
import com.ayf.payment.game.api.service.PartitionService;
import com.ayf.payment.game.api.utils.DateUtils;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.ayf.payment.game.api.utils.http.MyHttpUtil;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: 发送爱游宝通知信息工具类
 * @Author: zh-liang
 * @Date : 2021-03-17 11:16
 */
@Component
public class AybSendMsgUtils {

    @Autowired
    private PartitionService partitionService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantSonService merchantSonService;
    @Autowired
    private AybUserService aybUserService;

    private static final String AYB_PUSH_URL = System.getenv("AYB_SCAN_CALLBACK_URL") + "/ayb-api/p2/jpush";
    private static final String PLATFORM_CODE = System.getenv("GAME_AYFPAY_PLATFORM_CODE");

    public void bindAyb(AybUserDTO aybUserDTO) {
        String account = "";
        if (aybUserDTO.getType() == 1) {
            Merchant merchant = merchantService.find(aybUserDTO.getMerchantId());
            account = merchant.getLoginAccount();
        } else if (aybUserDTO.getType() == 2) {
            MerchantSon merchantSon = merchantSonService.selectInfo(aybUserDTO.getLoginId());
            account = merchantSon.getLoginAccount();
        }
        Map<String, Object> par = new HashMap<>();
        par.put("playerId", aybUserDTO.getAybId());
        par.put("title", "账号绑定成功提醒");
        par.put("alert", "您的GM账号与爱游宝账户绑定成功");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> extraMap = new HashMap<>();
        extraMap.put("k", "账号名称");
        extraMap.put("v", account);
        list.add(extraMap);
        Map<String, Object> extraMap2 = new HashMap<>();
        extraMap2.put("k", "绑定时间");
        extraMap2.put("v", DateUtils.dateToString(aybUserDTO.getBindTime()));
        list.add(extraMap2);

        par.put("extra", JSONObject.toJSONString(list));
        send(par);
    }

    private void send(Map<String, Object> par) {
        String traceId = SnowFlakeUtils.getTraceId();
        try {
            par.put("code", PLATFORM_CODE);
            HashMap<String, Object> data = MyHttpUtil.doPostFrom(traceId, AYB_PUSH_URL, par);
            Integer code = (Integer) data.get(MyHttpUtil.STATUS_CODE);
            if (code != 200) {
                LogPortal.error("{}爱游宝账户：{}爱游宝消息推送失败，响应code不是200", traceId);
                return;
            }
            String result = (String) data.get(MyHttpUtil.RESPONSE_ENTITY);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if ("200".equals(jsonObject.get("code").toString())) {
                LogPortal.info("{}爱游宝账户：{}爱游宝消息推送成功", traceId);
                return;
            }
            LogPortal.error("{}爱游宝账户：{}爱游宝消息推送失败，返回：{}", traceId, result);
        } catch (Exception e) {
            LogPortal.error("{}爱游宝账户：{}爱游宝消息推送失败，发生：{}异常，异常信息为：{}", e, traceId, e.getClass().getSimpleName(), e.getMessage());
        }
    }

    public void unbindAyb(AybUserDTO aybUserDTO) {
        String account = "";
        if (aybUserDTO.getType() == 1) {
            Merchant merchant = merchantService.find(aybUserDTO.getMerchantId());
            account = merchant.getLoginAccount();
        } else if (aybUserDTO.getType() == 2) {
            MerchantSon merchantSon = merchantSonService.selectInfo(aybUserDTO.getLoginId());
            account = merchantSon.getLoginAccount();
        }
        Map<String, Object> par = new HashMap<>();
        par.put("playerId", aybUserDTO.getAybId());
        par.put("title", "解绑成功提醒");
        par.put("alert", "您的GM账号与爱游宝账户解绑成功");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> extraMap = new HashMap<>();
        extraMap.put("k", "账号名称");
        extraMap.put("v", account);
        list.add(extraMap);
        Map<String, Object> extraMap3 = new HashMap<>();
        extraMap3.put("k", "解绑类型");
        extraMap3.put("v", "爱游宝扫码解绑");
        list.add(extraMap3);
        Map<String, Object> extraMap2 = new HashMap<>();
        extraMap2.put("k", "解绑时间");
        extraMap2.put("v", DateUtils.dateToString(new Date()));
        list.add(extraMap2);

        par.put("extra", JSONObject.toJSONString(list));
        send(par);

    }

    public void login(Integer merchantId, Integer loginId, String accountName, String loginType, String loginIp) {
        AybUser bindAybUser = aybUserService.getBindAybUser(merchantId, loginId);
        if (bindAybUser == null || bindAybUser.getLoginRemindStatus() == 0) {
            return;
        }
        LogPortal.info("商户号【{}】爱游宝登录提醒开始发送，登录信息【loginId:{},loginAccount:{},loginType:{},loginIp:{}】", merchantId, loginId, accountName, loginType,loginIp);
        Map<String, Object> par = new HashMap<>();
        par.put("playerId", bindAybUser.getAybId());
        par.put("title", "登录成功提醒");
        par.put("alert", "您的GM账号登录成功");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> extraMap = new HashMap<>();
        extraMap.put("k", "账号名称");
        extraMap.put("v", accountName);
        list.add(extraMap);
        Map<String, Object> extraMap3 = new HashMap<>();
        extraMap3.put("k", "登录方式");
        extraMap3.put("v", loginType);
        list.add(extraMap3);
        Map<String, Object> extraMap2 = new HashMap<>();
        extraMap2.put("k", "登录时间");
        extraMap2.put("v", DateUtils.dateToString(new Date()));
        list.add(extraMap2);
        Map<String, Object> extraMap4 = new HashMap<>();
        extraMap4.put("k", "登录IP");
        extraMap4.put("v", loginIp);
        list.add(extraMap4);
        Map<String, Object> extraMap5 = new HashMap<>();
        extraMap5.put("k", "备注");
        extraMap5.put("v", "如非本人登录，请及时修改密码");
        list.add(extraMap5);

        par.put("extra", JSONObject.toJSONString(list));
        send(par);
    }

    public void rechargeFail(String first, String remark, String traceId, Order order) {
        if (order.getWxNotice() == 2) {
            return;
        }
        List<AybUser> aybUserList = aybUserService.getBindAybUser(order.getMerchantId());
        if (CollUtil.isEmpty(aybUserList)) {
            return;
        }
        String amount = String.format("%.2f", order.getAmount());
        String k2 = "订单号【" + order.getMerchantOrderNo() + "】，充值金额【" + amount + "元】，充值时间【" + DateUtils.dateToString(order.getCreateTime()) + "】";
        String k3 = order.getPartitionName() + "【" + order.getPartitionId() + "】";
        for (AybUser aybUser : aybUserList) {
            if (aybUser.getExceptionRemindStatus() == 0) {
                continue;
            }
            Map<String, Object> par = new HashMap<>();
            par.put("playerId", aybUser.getAybId());
            par.put("title", "充值异常提醒");
            par.put("alert", first);

            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> extraMap = new HashMap<>();
            extraMap.put("k", "玩家账号");
            extraMap.put("v", order.getRechargeNumber());
            list.add(extraMap);
            Map<String, Object> extraMap3 = new HashMap<>();
            extraMap3.put("k", "订单信息");
            extraMap3.put("v", k2);
            list.add(extraMap3);
            Map<String, Object> extraMap2 = new HashMap<>();
            extraMap2.put("k", "分区信息");
            extraMap2.put("v", k3);
            list.add(extraMap2);
            Map<String, Object> extraMap4 = new HashMap<>();
            extraMap4.put("k", "错误码");
            extraMap4.put("v", traceId);
            list.add(extraMap4);
            Map<String, Object> extraMap5 = new HashMap<>();
            extraMap5.put("k", "备注");
            extraMap5.put("v", remark);
            list.add(extraMap5);

            par.put("extra", JSONObject.toJSONString(list));
            send(par);
        }
    }

    public void rechargeSuccess(Order order,String remark) {
        if (order.getWxNotice() == 2) {
            return;
        }
        List<AybUser> aybUserList = aybUserService.getBindAybUser(order.getMerchantId());
        if (CollUtil.isEmpty(aybUserList)) {
            return;
        }
        PartitionDTO partitionDTO = partitionService.getPartitionInfoById(order.getPartitionId());
        String amount = String.format("%.2f", order.getAmount());
        String k5 = "游戏分区【" + partitionDTO.getPartitionName() + "】，充值时间【" + DateUtils.dateToString(order.getCreateTime()) + "】";
        Merchant merchant = merchantService.find(order.getMerchantId());
        String first = "您的账户：【" + merchant.getLoginAccount() + "】有一笔新订单";
        for (AybUser aybUser : aybUserList) {
            if (aybUser.getRechargeRemindStatus() == 0) {
                continue;
            }
            Map<String, Object> par = new HashMap<>();
            par.put("playerId", aybUser.getAybId());
            par.put("title", "充值提醒");
            par.put("alert", first);

            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> extraMap = new HashMap<>();
            extraMap.put("k", "商户订单号");
            extraMap.put("v", order.getMerchantOrderNo());
            list.add(extraMap);
            Map<String, Object> extraMap3 = new HashMap<>();
            extraMap3.put("k", "商户账号");
            extraMap3.put("v", merchant.getLoginAccount() + "【" + merchant.getId() + "】");
            list.add(extraMap3);
            Map<String, Object> extraMap2 = new HashMap<>();
            extraMap2.put("k", "玩家账号");
            extraMap2.put("v", order.getRechargeNumber());
            list.add(extraMap2);
            Map<String, Object> extraMap4 = new HashMap<>();
            extraMap4.put("k", "充值金额");
            extraMap4.put("v", amount);
            list.add(extraMap4);
            Map<String, Object> extraMap5 = new HashMap<>();
            extraMap5.put("k", "充值详情");
            extraMap5.put("v", k5);
            list.add(extraMap5);
            Map<String, Object> extraMap6 = new HashMap<>();
            extraMap6.put("k", "备注");
            extraMap6.put("v",remark);
            list.add(extraMap6);

            par.put("extra", JSONObject.toJSONString(list));
            send(par);
        }
    }

    public void createPartitionFail(String traceId, Integer merchantId, String first, String partitionId) {
        List<AybUser> aybUserList = aybUserService.getBindAybUser(merchantId);
        if (CollUtil.isEmpty(aybUserList)) {
            return;
        }
        PartitionDTO partitionDTO = partitionService.getPartitionInfoById(partitionId);
        String k1 = partitionDTO.getPartitionName() + "【" + partitionId + "】";
        String k2 = partitionDTO.getServerIp() + ":" + partitionDTO.getPortNum();
        for (AybUser aybUser : aybUserList) {
            if (aybUser.getRechargeRemindStatus() == 0) {
                continue;
            }
            Map<String, Object> par = new HashMap<>();
            par.put("playerId", aybUser.getAybId());
            par.put("title", "分区创建异常提醒");
            par.put("alert", first);

            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> extraMap = new HashMap<>();
            extraMap.put("k", "分区名称");
            extraMap.put("v", k1);
            list.add(extraMap);
            Map<String, Object> extraMap3 = new HashMap<>();
            extraMap3.put("k", "网关所在服务器");
            extraMap3.put("v", k2);
            list.add(extraMap3);
            Map<String, Object> extraMap2 = new HashMap<>();
            extraMap2.put("k", "安装路径");
            extraMap2.put("v", partitionDTO.getInstallPath());
            list.add(extraMap2);
            Map<String, Object> extraMap4 = new HashMap<>();
            extraMap4.put("k", "创建时间");
            extraMap4.put("v", DateUtils.dateToString(new Date()));
            list.add(extraMap4);
            Map<String, Object> extraMap5 = new HashMap<>();
            extraMap5.put("k", "错误码");
            extraMap5.put("v", traceId);
            list.add(extraMap5);
            Map<String, Object> extraMap6 = new HashMap<>();
            extraMap6.put("k", "备注");
            extraMap6.put("v", "本次异常提醒24小时内只会提醒一次，请及时处理");
            list.add(extraMap6);

            par.put("extra", JSONObject.toJSONString(list));
            send(par);
        }
    }
}
